<template>
    <div>
        <div style="margin: 10px 0">
            <el-select v-model="displayConference" placeholder="请选择东西部">
                <el-option
                        v-for="item in options"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value">
                </el-option>
            </el-select>
            <el-input style="width: 150px;margin-left: 5px" placeholder="请输入球队名称" suffix-icon="el-icon-search" v-model.trim="displayAbbr"></el-input>
            <el-input style="width: 150px;margin-left: 5px" placeholder="请输入所在分区" suffix-icon="el-icon-search" v-model.trim="division"></el-input>
            <el-input style="width: 150px;margin-left: 5px" placeholder="请输入所在城市" suffix-icon="el-icon-search" v-model.trim="city"></el-input>

            <el-button class="ml-5" type="primary" @click="load">搜索</el-button>
            <el-button type="warning" @click="reset">重置</el-button>
        </div>
        <div style="margin: 10px 0">

            <el-button type="primary" @click="openPoster">添加</el-button>


                <el-button type="danger" slot="reference" @click="delBatch">批量删除 </el-button>


        </div>
        <el-table :data="tableData" border stripe :header-cell-class-name="'headerBg'"
                  @selection-change="handleSelectionChange" size="medium">
            <el-table-column type="selection" width="55"></el-table-column>
            <el-table-column prop="id" label="ID" width="80"></el-table-column>
            <el-table-column
                    prop="abbr"
                    label="logo"
                    width="81px"
                    align="center"
            >
                <!--https://res.nba.cn/media/img/teams/logos/MIA_logo.svg-->
                <template slot-scope="scope">
                    <el-image   class="article-cover"
                                 :src="'https://res.nba.cn/media/img/teams/logos/'+scope.row.abbr+'_logo.svg'"
                                 :preview-src-list="['https://res.nba.cn/media/img/teams/logos/'+scope.row.abbr+'_logo.svg']"/>
                </template>
            </el-table-column>
            <el-table-column prop="city" label="城市" width="150px"></el-table-column>
            <el-table-column prop="displayAbbr" label="球队" width="120px"></el-table-column>
            <el-table-column prop="division" label="分区" width="120px"></el-table-column>
            <el-table-column prop="createTime" label="建队时间" :formatter="dateFormat"></el-table-column>
            <el-table-column prop="nameEn" label="球队(英文名)" width="120px"></el-table-column>
            <el-table-column prop="cityEn" label="城市(英文名)" width="120px"></el-table-column>
            <el-table-column prop="displayConference" label="东/西部" width="100px"></el-table-column>



            <el-table-column label="操作" width="100" align="center">

                <template slot-scope="scope">

                    <el-button type="primary" @click="edit(scope.row)" icon="el-icon-edit" circle></el-button>

                    <el-button @click="del(scope.row.id)" title="删除"
                               slot="reference" type="danger" icon="el-icon-delete" circle
                               style="margin-left: 10px"></el-button>

                </template>
            </el-table-column>
        </el-table>

        <div style="padding: 10px 0">
            <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page="pageNum"
                    :page-sizes="[2, 5, 10, 20]"
                    :page-size="pageSize"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="total">
            </el-pagination>
        </div>

        <!--弹窗-->

        <el-dialog
                title="更新球队"
                :visible.sync="dialogVisible"
                width="30%">
            <el-form :model="form" label-width="120px">
                <el-form-item label="球队">
                    <el-input v-model="form.displayAbbr"autocomplete="off"></el-input>
                </el-form-item>


                <el-form-item label="城市">
                    <el-input   maxlength="40" v-model="form.city" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="分区">
                    <el-input v-model="form.division" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="建队时间">
                    <el-date-picker
                            v-model="form.createTime"
                            type="datetime"
                            placeholder="选择日期时间">
                    </el-date-picker>

                </el-form-item>
                <el-form-item label="球队(英文名)">
                    <el-input v-model="form.nameEn" autocomplete="off"></el-input>
                </el-form-item>

                <el-form-item label="城市(英文名)">
                    <el-input v-model="form.cityEn" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="东/西部">
                    <el-input v-model="form.displayConference" autocomplete="off"></el-input>
                </el-form-item>


            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="save">确 定</el-button>
            </div>

        </el-dialog>
        <!--新增-->
        <el-dialog
                title="新增球队"
                :visible.sync="addDialogVisible"
                width="30%">
            <el-form :model="forms" label-width="120px"  :rules="rules" ref="forms">
                <el-form-item label="球队" prop="displayAbbr">
                    <el-input v-model="forms.displayAbbr"autocomplete="off"></el-input>
                </el-form-item>


                <el-form-item label="城市" prop="city">
                    <el-input   maxlength="40" v-model="forms.city" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="分区" prop="division">
                    <el-input v-model="forms.division" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="建队时间" prop="createTime">
                    <el-date-picker
                            v-model="forms.createTime"
                            type="datetime"
                            placeholder="选择日期时间">
                    </el-date-picker>

                </el-form-item>
                <el-form-item label="球队(英文名)" prop="nameEn">
                    <el-input v-model="forms.nameEn" autocomplete="off"></el-input>
                </el-form-item>

                <el-form-item label="城市(英文名)" prop="cityEn">
                    <el-input v-model="forms.cityEn" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="东/西部" prop="displayConference">
                    <el-input v-model="forms.displayConference" autocomplete="off"></el-input>
                </el-form-item>


            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button  @click="resetForm('forms')">取 消</el-button>
                <el-button type="primary" @click="addTeam">确 定</el-button>
            </div>

        </el-dialog>




    </div>
</template>

<script>
    import request from '../utils/request'
    import {serverIp} from "../../public/config";

    export default {
        name: "Team",

        data() {
            return {
                options: [{
                    value: '东部',
                    label: '东部'
                }, {
                    value: '西部',
                    label: '西部'
                }],
                comment:'',
                serverIp:serverIp,
                dialogImageUrl: '',
                dialogVisible1: false,
                rules: {
                    displayAbbr: [
                        {required: true, message: '请填写球队名称', trigger: 'blur'},
                        {min: 3, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur'}
                    ],
                    city: [
                        {required: true, message: '请输入城市名', trigger: 'blur'},
                        {min: 1, max: 20, message: '长度在 2 到 10 个字符', trigger: 'blur'}
                    ],
                    division: [
                        {required: true, message: '请输入分区名称', trigger: 'blur'},
                        {min: 1, max: 20, message: '长度在 2 到 10 个字符', trigger: 'blur'}
                    ],
                    createTime: [
                        {required: true, message: '请输入建队时间', trigger: 'blur'},

                    ],
                    division: [
                        {required: true, message: '请输入球队(英文名)', trigger: 'blur'},
                        {min: 1, max: 20, message: '长度在 2 到 15 个字符', trigger: 'blur'}
                    ],
                    nameEn: [
                        {required: true, message: '请输入城市(英文名)', trigger: 'blur'},
                        {min: 1, max: 20, message: '长度在 2 到 15 个字符', trigger: 'blur'}
                    ],
                    displayConference: [
                        {required: true, message: '请输入东/西部', trigger: 'blur'},
                        {min: 1, max: 20, message: '长度在 2 到 5 个字符', trigger: 'blur'}
                    ],

                },
                forms:{},
                form: {},
                addDialogVisible:false,
                serverIp: serverIp,
                dialogImageUrl: '',
                dialogVisible: false,
                tableData: [],
                displayAbbr: '',
                city: '',
                displayConference: '',
                division: '',

                multipleSelection: [],
                pageNum: 1,
                pageSize: 5,
                total: 0
            }
        },
        created() {
            this.load()
        },
        methods: {

            dateFormat(row, column, cellValue, index) {

                const daterc = row[column.property]
                if (daterc != null) {
                    var date = new Date(daterc);
                    var year = date.getFullYear();
                    /* 在日期格式中，月份是从0开始，11结束，因此要加0
                     * 使用三元表达式在小于10的前面加0，以达到格式统一  如 09:11:05
                     * */
                    var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
                    var day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
                    var hours = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
                    var minutes = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
                    var seconds = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
                    // 拼接
                    return year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds;
                }
            },
            resetForm(formName) {
                console.log(formName)
                this.$nextTick(()=>{
                    this.$refs.forms.resetFields();
                })

                this.addDialogVisible=false

            },
            //新增
            addTeam(){
                this.$refs['forms'].validate((valid) => {
                    if (valid) {  // 表单校验合法

                        request.post('/team/addTeam',this.forms).then(res=>{
                            if (res.code==='200'){
                                this.$message({message: '添加成功', type: "success"})
                                this.load()
                                this.addDialogVisible = false
                                this.forms={}
                            } else{
                                this.$message({message: '添加失败', type: "error"})
                            }

                        })
                    }})

            },

            openPoster(){
                this.addDialogVisible=true
            },
            //更新
            save() {
                request.put('/team/updateTeam', this.form).then(res => {
                    if (res.code === '200') {
                        this.$message({message: '更新成功', type: "success"})
                        this.load()
                        this.dialogVisible = false
                    } else {
                        this.$message({message: '更新失败', type: "success"})
                    }
                })
            },

//编辑
            edit(row) {

                this.form = row
                this.dialogVisible = true
            },

            load() {
                request.get("/team/page", {
                    params: {
                        pageNum: this.pageNum,
                        pageSize: this.pageSize,
                        displayAbbr:this.displayAbbr,
                        division:this.division,
                        displayConference:this.displayConference,
                        city:this.city
                    }
                }).then(res => {

                    this.tableData = res.data.records
                    this.total = res.data.total

                })
            },
            changeEnable(row) {

                request.put("/poster/updatePoster", row).then(res => {
                    if (res.code === '200') {
                        this.$message({message: '操作成功', type: "success"})
                    }
                })
            },
            del(id) {
                this.$confirm('你确定要删除,是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(async() => {
                    request.delete("/team/deleteTeam/" + id).then(res => {
                        if (res.code === '200') {
                            this.$message({message: '删除成功', type: "success"})
                            this.load()
                        } else {
                            this.$message({message: '删除失败', type: "error"})
                        }
                    })}).catch(() => {
                    this.$message({
                        message: '已取消删除',
                        type: 'success'
                    })
                })
            },
            handleSelectionChange(val) {

                this.multipleSelection = val

            },
            delBatch() {
                this.$confirm('你确定要删除,是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(async() => {
                    let ids = this.multipleSelection.map(v => v.id)  // [{}, {}, {}] => [1,2,3]
                    request.post("/team/del/batch", ids).then(res => {
                        if (res.code === '200') {
                            this.$message({message: '批量删除成功', type: "success"})
                            this.load()
                        } else {
                            this.$message({message: '批量删除失败', type: "error"})
                        }
                    })}).catch(() => {
                    this.$message({
                        message: '已取消删除',
                        type: 'success'
                    })
                })

            },
            reset() {
                this.displayConference = ""
                this.displayAbbr=""
                this.city=""
                this.division=""
                this.load()
            },
            handleSizeChange(pageSize) {
                console.log(pageSize)
                this.pageSize = pageSize
                this.load()
            },
            handleCurrentChange(pageNum) {
                console.log(pageNum)
                this.pageNum = pageNum
                this.load()
            },
            handleFileUploadSuccess(res) {
                console.log(res)
                this.$message({message: '上传成功', type: "success"})
                this.load()
            },
            download(url) {
                window.open(url)
            },
            /*        preview(url) {
                        window.open('https://file.keking.cn/onlinePreview?url=' + encodeURIComponent(window.btoa((url))))
                    },*/
            preview(index) {
                this.dialogImageUrl = index;
                this.dialogVisible1 = true;
            },
        }
    }
</script>

<style scoped>

</style>
